﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Vds2465Telegrams
{
    /// <summary>
    /// TODO clarify
    /// 1. AddressExtension 0x01 and 0x02 have two status message bytes? The second is bit-coded?
    /// AddressExtension 0x10 has 3 status message bytes?
    /// Preliminary solution: byte[] blockStateBytes
    /// 
    /// 2. Also missing: verification of constraint that address is dividable by 8
    /// 
    /// possible changes would also need to be done in UserData_26_BlockStateAll
    /// </summary>
    public class UserData_24_BlockState : UserData_AddressExt_1
    {
        #region private member variables

        private readonly byte[] _blockStateBytes;

        #endregion private member variables

        #region constructor

        public UserData_24_BlockState(byte device, byte address, byte addressAddOn, eAddressExtension_1 addressExtension, byte[] blockStateBytes)
            : base(UserData.UserData24BlockState, device, address, addressAddOn, addressExtension)
        {
            if (blockStateBytes != null)
			{
                _blockStateBytes = new byte[blockStateBytes.Length];
                for (int index = 0; index < blockStateBytes.Length; index++)
				{
                    _blockStateBytes[index] = blockStateBytes[index];
				}
			}
			else
			{
                _blockStateBytes = new byte[0];
			}
		}

		#endregion constructor

        #region getters

        public byte[] GetCopyOfBlockStateBytes()
        {
            byte[] retBlockStateBytes = new byte[_blockStateBytes.Length];
            for (int index = 0; index < _blockStateBytes.Length; index++)
			{
                retBlockStateBytes[index] = _blockStateBytes[index];
			}

            return retBlockStateBytes;
        }	
        
        #endregion getters

        #region ToString, Equals, GetHashCode

        public override int GetHashCode()
        {
            int hashCode = base.GetHashCode();

            hashCode ^= UserDataCommon.GetHashCode_ByteArray(_blockStateBytes);
						
			return hashCode;
        }

        public override string ToString()
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("(");
            stringBuilder.Append(ToStringProperties());
            stringBuilder.Append(")");

            return stringBuilder.ToString();
        }

        internal override string ToStringProperties()
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(base.ToStringProperties());

            stringBuilder.Append(", ");
            stringBuilder.Append(UserDataCommon.HexByteArrayToString("BlockStateBytes", _blockStateBytes));

            return stringBuilder.ToString();
        }

        public override bool Equals(object obj)
        {
            Type objType = obj.GetType();
            if (this.GetType() == objType)
            {
                UserData_24_BlockState userData2Compare = obj as UserData_24_BlockState;
                return EqualsProperties(userData2Compare);
            }

            return false;
        }

        internal bool EqualsProperties(UserData_24_BlockState userData2Compare)
        {
            if (base.EqualsProperties(userData2Compare))
            {
                return UserDataCommon.Equals2ByteArrays(_blockStateBytes, userData2Compare.GetCopyOfBlockStateBytes());				
            }

            return false;
        }

        #endregion ToString, Equals, GetHashCode
    }
}
